package lablayer.model.MathComponents;

import lablayer.model.LabData;

/**
 * @author Darkness
 */

/*
 * Component #16
 *
 * Empirical frequency of j-th interval
 */
public class EmpiricalFrequency implements MathComponent {

	private LabData result;

	private LabData data;

	private String componentName = "Эмпирическая частота";

	private String componentId = "16";

	public EmpiricalFrequency() {
		super();
		this.result = new LabData();
	}

	@Override
	public LabData getData() {
		return this.data;
	}

	@Override
	public void calculate() {
		try {
			Double[] row = this.data.getData()[0][0];
			Double intervalCount = this.data.getData()[0][1][0];
			Double[] result = new Double[intervalCount.intValue()];
			Double intervalLength = 0.0;
			Double leftBorder = 0.0;
			MathComponent intLengthCom = MathComponentFactory.createIntervalLength();
			MathComponent minValueCom = MathComponentFactory.createMinValue();

			intLengthCom.setData(this.data);
			intLengthCom.calculate();
			intervalLength = intLengthCom.getResult().getData()[0][0][0];
			minValueCom.setData(this.data);
			minValueCom.calculate();
			leftBorder = minValueCom.getResult().getData()[0][0][0];

			for (int i = 0; i < intervalCount; i++) {
				int valuesCount = 0;
				if (i != (intervalCount - 1)) {
					for (int j = 0; j < row.length; j++) {
						if ((row[j] >= leftBorder) && (row[j] < (leftBorder + intervalLength))) {
							valuesCount++;
						}
					}
				} else {
					for (int j = 0; j < row.length; j++) {
						MathComponent maxValueCom = MathComponentFactory.createMaxValue();
						maxValueCom.setData(this.data);
						maxValueCom.calculate();
						Double maxValue = maxValueCom.getResult().getData()[0][0][0];
						if ((row[j] >= leftBorder) && (row[j] <= maxValue)) {
							valuesCount++;
						}
					}
				}
				result[i] = (double)valuesCount / row.length;
				leftBorder += intervalLength;
			}
			this.result = new LabData(result);
		} catch (Exception e) {

		}
	}

	@Override
	public void setData(LabData data) {
		try {
			assert data != null : "";
			this.data = data;
		} catch (Exception e) {}
	}

	@Override
	public String getComponentId() {
		return this.componentId;
	}

	@Override
	public String getComponentName() {
		return this.componentName;
	}

	@Override
	public LabData getResult() {
		return this.result;
	}

	@Override
	public void setParameter(String params, String values) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public LabData getInitData() {
		throw new UnsupportedOperationException("Not supported yet.");
	}

}
